<div id="app">
    <section class="content-header">
        <h1>
            <small>分类添加</small>
        </h1>

    </section>

    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-info">
                    <div class="box-header with-border">


                        <div class="box-tools">
                            <div class="btn-group pull-right" style="margin-right: 5px">
                                <a href="/admin/category/index" class="btn btn-sm btn-default" title="分类列表"><i class="fa fa-list"></i><span class="hidden-xs">&nbsp;分类列表</span></a>
                            </div>
                        </div>
  <span>
    <a class="btn btn-sm btn-primary grid-refresh" title="刷新"><i class="fa fa-refresh"></i><span class="hidden-xs"> 刷新</span></a>
    <a class="btn btn-sm btn-primary" onclick="javascript:history.go(-1);" title="返回"> <span class="hidden-xs"> 返回</span></a>
  </span>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form id="addForm" action="/admin/goods/goodsAdd" method="post" accept-charset="UTF-8" class="form-horizontal" enctype="multipart/form-data" pjax-container>
                        <div class="box-body">
                            <div class="fields-group">
                                <div class="form-group  ">
                                    <label for="cha_id" class="col-sm-2  control-label">渠道选择</label>
                                    <div class="col-sm-8">
                                        <select class="form-control cha_id" id="cha_id" style="width: 100%;" name="cha_id" data-value="" onchange="getPid(this)" >
                                            <option value="0">平台</option>
                                            @if($channel)
                                            @foreach($channel as $k=>$v)
                                            <option value="{{$k}}" >{{$v}}</option>
                                            @endforeach
                                            @endif
                                        </select>
                                    </div>
                                </div>


                                <div  class="form-group pid-div"  >
                                    <label for="pid" class="col-sm-2  control-label">上级分类</label>
                                    <div class="col-sm-8">

                                        <select id="pid" class="form-control pid" style="width: 50%;" name="pid"  >
                                            <option value="0">顶级分类</option>


                                        </select>
                                    </div>
                                </div>



                                <div class="form-group  ">
                                    <label for="cate_name" class="col-sm-2  control-label">分类名称</label>
                                    <div class="col-sm-8">
                                        <div class="input-group">
                                            <span class="input-group-addon"><i class="fa fa-pencil fa-fw"></i></span>
                                            <input type="text" id="cate_name" name="cate_name" value="" class="form-control cate_name" placeholder="输入 分类名称" />
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group  ">
                                    <label for="cate_note" class="col-sm-2  control-label">分类简单阐释</label>
                                    <div class="col-sm-8">
                                        <div class="input-group">
                                            <span class="input-group-addon"><i class="fa fa-pencil fa-fw"></i></span>
                                            <input style="width:30%" type="text" id="cate_note" name="cate_note" value="" class="form-control cate_note" placeholder="输入 分类简单阐释" />
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group  ">
                                    <label for="cate_code" class="col-sm-2  control-label">类目码</label>
                                    <div class="col-sm-8">
                                        <div class="input-group">
                                            <span class="input-group-addon"><i class="fa fa-pencil fa-fw"></i></span>
                                            <input type="text" id="cate_code" name="cate_code" value="" class="form-control cate_code" placeholder="输入 类目码" />
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group  ">
                                    <label for="cate_code_name" class="col-sm-2  control-label">类目名称</label>
                                    <div class="col-sm-8">
                                        <div class="input-group">
                                            <span class="input-group-addon"><i class="fa fa-pencil fa-fw"></i></span>
                                            <input type="text" id="cate_code_name" name="cate_code_name" value="" class="form-control cate_code_name" placeholder="输入 类目名称" />
                                        </div>
                                    </div>
                                </div>


                                <div class="form-group  ">
                                    <label for="img" class="col-sm-2  control-label">分类logo</label>
                                    <div class="col-sm-8">
                                        <input type="file" class="img" name="img"  />
                                    </div>
                                </div>
                                <div class="form-group  ">
                                    <label for="cate_hot_search" class="col-sm-2  control-label">热门搜索标签</label>
                                    <div class="col-sm-8">
                                        <textarea name="cate_hot_search" class="form-control cate_hot_search" rows="5" placeholder="输入 热门搜索标签"  ></textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- /.box-body -->

                        <div class="box-footer">
                            @csrf
                            <div class="col-md-2">
                            </div>
                            <div class="col-md-8">
                                <div class="btn-group pull-right">
                                    <button type="button" onclick="tijiao()" class="btn btn-primary">提交</button>
                                </div>

                                <div class="btn-group pull-left">
                                    <button type="reset" class="btn btn-warning">重置</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>

            </div></div>
    </section>
</div>
<script data-exec-on-popstate>
    $(function () {
        //刷新
        $('.grid-refresh').on('click', function() {
            $.pjax.reload('#pjax-container');
            toastr.success('刷新成功 !');
        });

        $("input.img").fileinput({"overwriteInitial":true,"initialPreviewAsData":true,"browseLabel":"\u6d4f\u89c8","showRemove":false,"showUpload":false,"dropZoneEnabled":false,"deleteExtraData":{"goods_img_list":"_file_del_","_file_del_":"","_token":"Kq6b1qbgSwE02ACOgZOsO9QeD99PSWROQgk10d1w","_method":"PUT"},"deleteUrl":"http:\/\/www.laras.com\/admin\/goods\/","allowedFileTypes":["image"]});

        var cha_id=$("#cha_id").val();
        if(cha_id>=0){
            getPid(null,cha_id);
        }
    });
    function getPid(ob,p=-1){
        var id=$(ob).val();
        console.log(id);
        console.log(p);
        if(p>=0)id=p;
        $.ajax({
            url:"/admin/category/getCategory",
            type:'post',
            data:{cha_id:id,_token:LA.token},
            dataType:'json',
            success:function(m){
                if(m.status==1){
                    $("#pid").html(m.data.html);
                }else{
                    $("#pid").html("<option value='0'>顶级分类</option>");
                    swal(m.message, '', 'error');
                }
            }
        });

    }

    /*function getPid(ob,p=0){
        var id=$(ob).val();

        if(p>0)id=p;
        if(id==0){
            $(".pid-div").hide();
        }
        console.log(id);
        if(id>0){
            $(".pid-div").show();
            $.ajax({
                url:"/admin/category/getCategory",
                type:'post',
                data:{cha_id:id,_token:LA.token},
                dataType:'json',
                success:function(m){
                    if(m.status==1){
                        $("#pid").html(m.data.html);
                    }else{
                        swal(m.message, '', 'error');
                    }
                }
            });
        }
    }*/

    function tijiao(){
        var data=new FormData($("#addForm")[0]);
        $.ajax({
            url:"/admin/category/add",
            type:'post',
            data:data,
            dataType:'json',
            processData: false,
            contentType: false,
            success:function(m){
                if(m.status==1){
                    toastr.success(m.message);
                    window.location.href='/admin/category/index';
                }else{
                    swal(m.message, '', 'error');
                }
            }
        });
    }
</script>